package weatherwidget.isrefluxer.bird.com.birdweatherwidget.service;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import weatherwidget.isrefluxer.bird.com.birdweatherwidget.constant.WeatherConstant;
import weatherwidget.isrefluxer.bird.com.birdweatherwidget.until.ConnectUtil;
import weatherwidget.isrefluxer.bird.com.birdweatherwidget.until.LogUtil;
import weatherwidget.isrefluxer.bird.com.birdweatherwidget.until.Utils;

/**
 * Created by Administrator on 2016/11/20 0020.
 */

public class LocationController {
    private static  final String TAG = "LocationController";
    public static final long LOCATION_TIMEOUT = 1000 * 60;
    LocationClient mLocationClient;
    Context context;
    Handler handler;
    private boolean active_fresh = false;
    boolean isLocationSuccess = false;
    public static LocationController locationController;
    public static LocationController getInstance(Context context,Handler handler) {
        if (locationController == null) {
            synchronized (LocationController.class) {
                if (locationController == null) {
                    locationController = new LocationController(context,handler);
                }
            }
        }
        return locationController;
    }
    private LocationController(Context context, Handler handler) {
        this.context = context;
        mLocationClient = new LocationClient(context);

        mLocationClient.registerLocationListener(new MyLocationListenner());
        this.handler = handler;
    }

    public void startLocation(boolean isfresh) {
        LogUtil.log(TAG,"startLocation-- isNetworkConnected:"+ConnectUtil.isNetworkConnected(context));
        if(!ConnectUtil.isNetworkConnected(context))
            return;
        this.active_fresh = isfresh;
        isLocationSuccess = false;
        setLocationOption();
        mLocationClient.start();

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                   if(!isLocationSuccess){
                       Intent intent = new Intent(WeatherConstant.ACTION_WEATHER_LOCATION_FAILURE);
                       intent.putExtra(WeatherConstant.DATA_ACTIVE_FRESH_LOCATION,LocationController.this.active_fresh);
                       context.sendBroadcast(intent);
                       try {
                           mLocationClient.stop();
                       } catch (Exception e) {
                       }
                   }
            }
        },LOCATION_TIMEOUT);
    }

    private void setLocationOption() {
        LogUtil.log(TAG,"setLocationOption");
        LocationClientOption mOption = new LocationClientOption();


        mOption = new LocationClientOption();
        mOption.setLocationMode(LocationClientOption.LocationMode.Battery_Saving);
        mOption.setCoorType("bd09ll");
        mOption.setScanSpan(3000);
        mOption.setIsNeedAddress(true);
        mOption.setIsNeedLocationDescribe(true);
        mOption.setNeedDeviceDirect(false);
        mOption.setLocationNotify(false);
        mOption.setIgnoreKillProcess(true);
        mOption.setIsNeedLocationDescribe(true);
        mOption.setIsNeedLocationPoiList(true);
        mOption.SetIgnoreCacheException(false);

        mOption.setIsNeedAltitude(false);



        mLocationClient.setLocOption(mOption);
    }

    private class MyLocationListenner implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            LogUtil.log(TAG,"location==null: "+(location == null));

            if (location == null)
                return;
            String city = location.getCity();

            LogUtil.log(TAG,"city== "+city+" location.getLocType(): "+location.getLocType());
          //  String province = location.getProvince();
            city = "深圳市";
            if (!TextUtils.isEmpty(city)) {
                Intent intent = new Intent(
                        WeatherConstant.ACTION_MORNCLOUD_LOCATION_SUCCESS);
                if (city.endsWith("市") && city.length() > 2) {
                    city = city.substring(0, city.length() - 1);
                }
              //  city = Utils.formatProvince(province) + "|" + city;
                intent.putExtra("city", city);
                intent.putExtra(WeatherConstant.DATA_ACTIVE_FRESH_LOCATION, active_fresh);
                context.sendBroadcast(intent);
                isLocationSuccess = true;
                LogUtil.log(TAG,"ACTION_MORNCLOUD_LOCATION_SUCCESS");
                mLocationClient.stop();
            }
        }

    }
}
